<?php
declare (strict_types=1);

namespace app\controller;

use app\facade\Param;
use app\handler\Excel;
use app\helpers\api\ApiResponse;
use app\model\Signup;
use think\Request;

class SignupController
{
    use ApiResponse;

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $query = Signup::order('create_time', 'desc');
        $query = Param::signupParam($request->param(), $query);
        return $this->success($query->paginate($request->param('page_size', 15))->each(function ($item) use ($request) {
            $item->certificate = $request->host() . $item->certificate;
        }));
    }

    public function show(Signup $signup)
    {
        return $this->success($signup);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $exist = Signup::where('planId', $request->param('planId'))->where('IDCard', $request->param('IDCard'))->findOrEmpty();
        if (!$exist->isEmpty()) {
            return $this->error('该用户已经报名了这次计划');
        }
        Signup::create($request->param());
        return $this->created();
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request, Signup $signup)
    {
        $signup->update($request->param());
        return $this->update();
    }

    /**
     * 删除
     * @param Signup $signup
     * @return mixed
     */
    public function delete(Signup $signup)
    {
        $signup->delete();
        return $this->destroy();
    }

    public function exportTemplate()
    {
        $datas = [
            [
                '计划ID',
                '时间',
                '费用',
                '考生姓名',
                '考生身份证号',
                '考生手机号',
                '类型',
                '岗位名称',
                '单位',
                '是否补考',
                '是否参加培训',
                '上传汇款凭证',
                '备注'
            ]
        ];
        $excel = new Excel();
        $excel->exportExcel($datas, '报名模板');
    }

    public function exportData(Request $request)
    {
        $key = [
                '计划ID',
                '时间',
                '费用',
                '考生姓名',
                '考生身份证号',
                '考生手机号',
                '类型',
                '岗位名称',
                '单位',
                '是否补考',
                '是否参加培训',
                '上传汇款凭证',
                '备注'
            ];
        $query = Signup::field('planId,testingTime,cost,studentName,IDCard,phone,type,post,companyName,makeup,training,certificate,remarks')->order('id', 'desc');
        $query = Param::signupParam($request->param(), $query);
        $datas = $query->select()->each(function ($item) use ($request) {
            if($item->certificate)$item->certificate = $request->host() . $item->certificate;
        })->toArray();
        array_unshift($datas,$key);
        $excel = new Excel();
        $excel->exportExcel($datas, '报名数据');
    }

    public function import(Request $request)
    {
        $file = request()->file('exl');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile('exl', $file);
        $exl      = new Excel();
        $data     = $exl->importExecl(app()->getRootPath() . 'init/storage/' . $savename);
        unset($data[1]);
        foreach ($data as $k => $value) {
            $exist = Signup::where('planId', $value['A'])->where('IDCard', $value['E'])->findOrEmpty();
            if (!$exist->isEmpty()) {
                continue;
            }
            Signup::create([
                'planId'      => $value['A'],
                'testingTime' => gmdate("Y/m/d h:i:s", \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value['B'])),
                'cost'        => $value['C'],
                'studentName' => $value['D'],
                'IDCard'      => $value['E'],
                'phone'       => $value['F'],
                'type'        => $value['G'],
                'post'        => $value['H'],
                'companyName' => $value['I'],
                'makeup'      => $value['J'],
                'training'    => $value['K'],
                'certificate' => $value['L'],
                'remarks'     => $value['M']
            ]);
        }
        unlink(app()->getRootPath() . 'init/storage/' . $savename);
        return $this->created();
    }

}
